﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Windows.Forms.Integration;
using Alsing.SourceCode;
using System.Reflection;
using System.Globalization;
using Alsing.Windows.Forms;

namespace UmlControlLibrary
{
    /// <summary>
    /// Interaction logic for SyntaxBoxTabControl.xaml
    /// </summary>
    
    public delegate void TextChangeDelegate(SyntaxDocument sender, string DocumentName);

    public partial class SyntaxBoxTabControl : UserControl
    {
        private Dictionary<string, SyntaxDocument> Documents = new Dictionary<string, SyntaxDocument>();
        private Dictionary<SyntaxDocument, string> DocumentNames = new Dictionary<SyntaxDocument, string>();

        private string SyntaxFile = "";

        public SyntaxBoxTabControl()
        {
            InitializeComponent();
        }

        private void SetDocumentSyntaxFile(SyntaxDocument document, string syntaxFile)
        {
            Assembly assembly = this.GetType().Assembly;
            AssemblyName assemblyName = assembly.GetName();
            string resourceName = String.Format(CultureInfo.InvariantCulture, "{0}.Resources.SyntaxFiles.{1}.syn", assemblyName.Name, syntaxFile);
            document.SetSyntaxFromEmbeddedResource(assembly, resourceName);
        }

        public void SetSyntaxFile(string syntaxFile)
        {
            SyntaxFile = syntaxFile;
            foreach (SyntaxDocument document in Documents.Values)
                SetDocumentSyntaxFile(document, syntaxFile);
        }

        public void AddItem(string name, string caption, string content)
        {
            SyntaxDocument document = new SyntaxDocument();
            SetDocumentSyntaxFile(document, SyntaxFile);
            document.Text = content;
            
            SyntaxBoxControl sbControl = new SyntaxBoxControl();
            sbControl.SplitView = false;
            sbControl.Document = document;

            sbControl.TextChanged += new EventHandler(sbControl_TextChanged);

            WindowsFormsHost host = new WindowsFormsHost();
            host.Child = sbControl;
            
            TabItem item = new TabItem();
            item.Header = caption;
            item.Content = host;

            Documents.Add(name, document);
            DocumentNames.Add(document, name);

            RootControl.Items.Add(item);
        }      

        public event TextChangeDelegate OnTextChanged;

        void sbControl_TextChanged(object sender, EventArgs e)
        {
            SyntaxDocument doc = (sender as SyntaxBoxControl).Document;
            if (OnTextChanged != null)
                OnTextChanged(doc, DocumentNames[doc]);
        }

        public string GetText(string name)
        {
            if (Documents.ContainsKey(name))
                return Documents[name].Text;
            else
                return "";
        }

        public void SetText(string name, string text)
        {
            if (!Documents.ContainsKey(name))
                AddItem(name, name, text);
            else
                Documents[name].Text = text;
        }

        public void SetWarningFlag(string name, bool flag)
        {
            SyntaxDocument doc = Documents[name];
            for (int i = 0; i < RootControl.Items.Count; i++)
                if ((((RootControl.Items[i] as TabItem).Content as WindowsFormsHost).Child as SyntaxBoxControl).Document == doc)
                {
                    //(RootControl.Items[i] as TabItem).HeaderTemplate;
                    break;
                }
        }
    }
}
